import {Component} from '@angular/core';
import {App, IonicPage, NavController, NavParams} from 'ionic-angular';
import {FormComponentService} from "../form-component/form-component.service";
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
import {HttpClient} from "@angular/common/http";
import {AppConfig} from "../../../app/appConfig";
import {AppUrl, TIME_OUT} from "../../../app/app.general";
import {CommonService} from "../../../app/common.service";

/**
 * Generated class for the BusinessAddPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-business-add',
  templateUrl: 'business-add.html',
})
export class BusinessAddPage {
  account =
    [
      {
        key: 'name',
        label: '商户名称',
        type: 'ion-input',
        inputType: 'text'
      },
      {
        key: 'addr',
        type: 'map',
        label: '商户地址',
        inputType: 'text'
      },
      {
        key: 'tel',
        type: 'ion-input',
        label: '联系电话',
        inputType: 'number'
      },
      {
        key: 'workrange',
        label: '经营范围',
        type: 'ion-select',
        options: [
          {
            key: '餐饮',
            val: 0
          }
        ]
      },
      {
        key: 'bankacc',
        label: '银行卡号',
        type: 'ion-input',
        inputType: 'number'
      },
    ];

  accountForm: FormGroup;

  constructor(public navCtrl: NavController, public navParams: NavParams, public serv: FormComponentService, public fb: FormBuilder, public http: HttpClient, public appConfig: AppConfig, public commonSer: CommonService) {
    this.appConfig.presentLoading('正在获取人员信息...');
    this.http.get(AppUrl.restaurant.getBossWithoutRestaurant).timeout(TIME_OUT).subscribe(res => {
      let result: any = res;
      if (result.code === 200) {
        let temp = {
          key: 'boosid',
          label: '商户老板',
          type: 'ion-select',
          options: result.data.map(item => {
            return {key: '姓名:' + item.name + ' ' + '账号:' + item.username, val: item.userid}
          })
        };
        this.account.push(temp);
      }
      else {
        this.appConfig.presentToast(result.msg);
      }

      this.appConfig.loader.dismiss();
    }, reject => {
      this.commonSer.httpError(reject);
      this.appConfig.loader.dismiss();
    });
    this.accountForm = this.fb.group({
      name: ['', [Validators.required, Validators.maxLength(20), Validators.minLength(2)]],
      addr: ['定位中,请勿在此时填写该项', [Validators.required, Validators.maxLength(100), Validators.minLength(4)]],
      tel: ['', [Validators.required, Validators.maxLength(15), Validators.minLength(5)]],
      workrange: [0, [Validators.required]],
      boosid: [null, [Validators.required]],
      bankacc: ['', [Validators.required,Validators.maxLength(18), Validators.minLength(15)]],
      note2: [null, [Validators.required]],
    });
    this.serv.group = this.accountForm;
  }

  ionViewDidLoad() {

    console.log('ionViewDidLoad BusinessAddPage');
  }

  output(val) {
    this.appConfig.presentLoading('正在申请...');
    this.http.post(AppUrl.restaurant.addRestaurant, val).subscribe(res => {
      let temp:any = res;
      if (temp.code === 200){
        this.navCtrl.pop();
      }
      else {
        this.appConfig.presentToast(temp.msg);
      }
      this.appConfig.loader.dismiss();
    }, reject => {
      this.commonSer.httpError(reject);
      this.appConfig.loader.dismiss();
    });
    console.log(val);
  }

}
